/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.xue.admin.system.mybatis.entity.SystemRoles;
import com.wyl.xue.admin.system.service.Menu;
import com.wyl.xue.admin.system.service.Role;
import com.wyl.xue.admin.system.vo.MenuTree;
import com.wyl.xue.admin.system.vo.RoleInfoVo;
import com.wyl.xue.core.util.result.WebResponse;
import com.wyl.xue.core.util.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName: RoleController
 * @Function: 角色信息接口
 * @Date: 2019/12/23 22:12
 * @author wyl
 * @version V1.0
 */
@RequestMapping("/admin/v1/role")
@Api(tags = {"角色信息接口"})
@AllArgsConstructor
@RestController
@CrossOrigin
public class RoleController {

    final Role role;
    final Menu menu;

    @ApiOperation(value = "添加角色信息", notes = "新建一个角色")
    @PostMapping(value = "/")
    @PreAuthorize("hasAuthority('sys:role:add')")
    public WebResult<Boolean> addRole(@Valid @RequestBody RoleInfoVo roleInfoVo) {
        role.addRole(roleInfoVo);
        return WebResponse.WebResponse.ok();
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改角色信息")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public WebResult<Boolean> changeRole(@RequestBody RoleInfoVo roleInfoVo) {
        role.updateRole(roleInfoVo);
        return WebResponse.WebResponse.ok();
    }


    @GetMapping(value = "/{id}/menus")
    @ApiOperation(value = "获取角色菜单")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public WebResult<List<Long>> getRoleMenus(@PathVariable Long id) {
        return WebResponse.WebResponse.ok(role.getRoleMenusByRoleId(id));
    }


    @GetMapping(value = "/menus/tree/all")
    @PreAuthorize("hasAnyAuthority('sys:role:add','sys:role:update')")
    @ApiOperation(value = "获取菜单树")
    public WebResult<List<MenuTree>> getMenusById() {
        return WebResponse.WebResponse.ok(menu.getMenuTree(-1L));
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "通过ID删除一个角色")
    @PreAuthorize("hasAuthority('sys:role:del')")
    public WebResult<Boolean> deleteRole(@PathVariable long id) {
        role.deleteRole(id);
        return WebResponse.WebResponse.ok();
    }


    @GetMapping(value = "/{page}/{size}")
    @ApiOperation(value = "获取所有角色信息分页")
    @PreAuthorize("hasAuthority('sys:role:get')")
    public WebResult<IPage<SystemRoles>> getRolesInfoByPage(@PathVariable Integer page, @PathVariable Integer size) {
        return WebResponse.WebResponse.ok(role.getRoleForPage(page, size));
    }

    @PostMapping(value = "/search")
    @ApiOperation(value = "获取菜单树 不包含按钮")
    @PreAuthorize("hasAuthority('sys:role:search')")
    public WebResult<List<SystemRoles>> search(@RequestBody RoleInfoVo roleInfoVo) {
        return WebResponse.WebResponse.ok(role.searchRole(roleInfoVo));
    }
}
